﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Common;
using Microsoft.TeamFoundation.Client;

namespace MirthToTFS
{
    public static class TFS
    {

        public static void CheckOutFile(string file)
        {
            var workspace = GetWorkspace(file);
            workspace.PendEdit(file);
        }

        public static void CheckInFile(string file)
        {
            var workspace = GetWorkspace(file);
            
            PendingChange[] pendingChanges = null;
            try
            {
                pendingChanges = workspace.GetPendingChanges()
                                          .Where(x => x.LocalItem.Equals(file, StringComparison.CurrentCultureIgnoreCase))
                                          .ToArray();
                workspace.CheckIn(pendingChanges, Config.NightlyCheckInComment);

            }
            catch (Exception) { }
        }

        public static Workspace GetWorkspace(string file)
        {
            WorkspaceInfo wi = Workstation.Current.GetLocalWorkspaceInfo(file);                     
            IServiceProvider lServiceProvider = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(wi.ServerUri);
            VersionControlServer lVC = lServiceProvider.GetService(typeof(VersionControlServer)) as VersionControlServer;
            return lVC.GetWorkspace(file);
        }


    }
}
